package com.mall4jcola.cloud.gateway;

import cn.hutool.core.util.StrUtil;
import com.mall4jcola.cloud.auth.gateway.UserAuthsQueryGateway;
import com.mall4jcola.cloud.auth.model.UserAuthEntity;
import com.mall4jcola.cloud.constant.IdentityTypeEnum;
import com.mall4jcola.cloud.convertor.UserAuthConvertor;
import com.mall4jcola.cloud.tunnel.database.dataobj.UserAuthsDO;
import com.mall4jcola.cloud.tunnel.database.repository.UserAuthsRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 有关登录账户信息表的查询操作
 * @author hmxchen
 */
@Slf4j
@Service
public class UserAuthsQueryGatewayImpl implements UserAuthsQueryGateway {
    @Resource
    private UserAuthsRepository userAuthsRepository;

    @Override
    public UserAuthEntity getByIdentityTypeAndIdentifier(String identityType, String identifier) {
        UserAuthEntity tokenEntity = UserAuthConvertor.toEntity(userAuthsRepository.findByIdentityTypeAndIdentifier(identityType, identifier).orElse(new UserAuthsDO()));
        if(StrUtil.isNotBlank(tokenEntity.getIdentifier())){
            if(IdentityTypeEnum.PHONE_NO_CODE.name().equals(tokenEntity.getIdentityType())){
                //从redis获取该手机号的验证码
                tokenEntity.setCredential("123456");
            }
        }
        return tokenEntity;
    }

    @Override
    public String getWxOpenIdByCode(String code) {
        return null;
    }
}
